﻿using System;
using System.Drawing;
using System.Windows.Forms;
using ClientApp.Classes;
using GianosNET.ClientApp.Properties;
using GianosNET.Common.Classes;


namespace ClientApp.Gestori
{
    public partial class Base : UserControl
    {
       internal ServiceInfoExt info;

       //public TileItem StackContainer { get; set; }
       public UC.NavItem ParentTile { get; set;}

       #region Eventi

        //public event ConnectionClosedHandler OnConnectionClosed;
        //public delegate void ConnectionClosedHandler(object sender, EventArgs args);
        
        public class ServiceEventArgs:EventArgs
        {
            public ServiceEventArgs(Guid svcId)
            {
                this.svcId = svcId;
            }

            public Guid svcId;
        }
        public delegate void ServiceCommandHandler(Base sender, ServiceEventArgs args);

        public event ServiceCommandHandler OnServiceStart;
        public event ServiceCommandHandler OnServiceStop;

        #endregion
        
        public Base()
        {
            InitializeComponent();
        }

        public ServiceInfo ServiceInformation
        {
            get
            {
                return info;
            }
        }

        internal virtual void SetServiceInformation(ServiceInfoExt info)
        {
            this.info = info;
            btnSystem.Text = string.Format("{0} v. {1} {2}       ", info.ServiceName, info.Version,
                                           !string.IsNullOrEmpty(info.InstanceName)
                                               ? string.Format("({0})", info.InstanceName)
                                               : "");

            ChangeServiceStatus(info.SvcId, info.ServiceStatus);
            ChangeLoopingStatus(info.SvcId, info.LoopingStatus);
        }

        private void Base_Load(object sender, System.EventArgs e)
        {
            
        }

        #region ctxSystem

        private void TSMIStart_Click(object sender, EventArgs e)
        {
            StartService();
            
        }

        private void TSMIStop_Click(object sender, EventArgs e)
        {
           StopService();
        }

        private void TSMIRestart_Click(object sender, EventArgs e)
        {
            //obj.Restart();
        }

        private void ctxSystem_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            TSMIStart.Visible = info.ActualServiceState != RemotedClass.RemotedClassStatusEnum.Started;
            TSMIStop.Visible = info.ActualServiceState == RemotedClass.RemotedClassStatusEnum.Started ||
                               info.ActualServiceState == RemotedClass.RemotedClassStatusEnum.Error;
        }

        #endregion

        public void StartService()
        {
            if (OnServiceStart != null)
                OnServiceStart(this, new ServiceEventArgs(info.SvcId));
        }
        public void StopService()
        {
            if (OnServiceStop != null)
                OnServiceStop(this, new ServiceEventArgs(info.SvcId));
        }

        #region Notifica di evento aggiornamento Log

        public virtual void AppendToLog(Guid logId, string msg) {}

        #endregion

        public virtual void ChangeServiceStatus(Guid id, RemotedClass.RemotedClassStatusEnum status)
        {
            btnSystem.Image = status == RemotedClass.RemotedClassStatusEnum.Started
                                  ? Resources.power_on
                                  : (status == RemotedClass.RemotedClassStatusEnum.Stopped
                                         ? Resources.power_off
                                         : Resources.warning);

            if (ParentTile != null)
                ParentTile.SetStatus(status);
        }

        public virtual void ChangeLoopingStatus(Guid id, LoopingService.LoopingStateEnum status)
        {
            switch (status)
            {
                case LoopingService.LoopingStateEnum.Running:
                    if (ParentTile != null)
                    {
                        ParentTile.SetBottomTitle("In esecuzione");
                        ParentTile.BackColor = Color.MediumSeaGreen;
                    }
                    break;
                case LoopingService.LoopingStateEnum.Stopped:
                    if (ParentTile != null)
                        ParentTile.SetBottomTitle("Non attivo");
                    break;
                case LoopingService.LoopingStateEnum.Idle:
                    if (ParentTile != null)
                    {
                        ParentTile.SetBottomTitle("In attesa");
                        ParentTile.BackColor = Color.Green;
                    }
                    break;
            }
        }

        public virtual void ChangeIdleCountdown(Guid id, int secBeforeNextExecution)
        {
            if (ParentTile != null)
                ParentTile.SetBottomTitle(string.Format("{0}\"", secBeforeNextExecution));
        }
    }
}
